package com.binance.notification.web.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.binance.notification.data.mapper.AbnormalTradeNoticeLogMapper;
import com.binance.notification.data.models.AbnormalTradeNoticeLogModel;
import com.binance.notification.web.service.IAbnormalTradeService;
import com.binance.notification.web.service.abnormaltrade.IAbnormalPriceService;
import com.binance.notification.web.service.abnormaltrade.IPriceBreakthroughsService;
import com.binance.notification.web.service.abnormaltrade.convert.AbnormalPricePushMessageConvertor;
import com.binance.notification.web.service.abnormaltrade.convert.AbnormalTradeNoticeLogConvertor;
import com.binance.notification.web.service.abnormaltrade.convert.PriceBreakthoughsPushMessageConvertor;
import com.binance.notification.web.service.abnormaltrade.enums.AbnromalTradeBusinessTypeEnum;
import com.binance.notification.web.service.abnormaltrade.models.AbnormalTradeNoticeLogBo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * IAbnormalTradeService是IAbnormalPriceService和IPriceBreakthroughsService高度聚合
 */
@Service
public class AbnormalTradeServiceImpl implements IAbnormalTradeService {

    @Autowired
    private AbnormalTradeNoticeLogMapper abnormalTradeNoticeLogMapper;

    @Autowired
    private IAbnormalPriceService abnormalPriceService;

    @Autowired
    private IPriceBreakthroughsService priceBreakthroughsService;

    @Override
    public String getAbnormalTradeNoticeInfoList(Integer pageIndex, Integer pageSize, String language) {
        Map<String, Object> param = Maps.newHashMap();
        if (pageIndex == null || pageIndex <= 0) {
            pageIndex = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 20;
        }
        param.put("start", (pageIndex - 1) * pageSize);
        param.put("size", pageSize);
        List<AbnormalTradeNoticeLogModel> list = abnormalTradeNoticeLogMapper.findByBusinessType(param);
        List<AbnormalTradeNoticeLogBo> boList = this.wrapNeedPushFlag(AbnormalTradeNoticeLogConvertor.toBoList(list));
        return this.wrapMessage(boList, this.getLanguage(language));
    }

    private String getLanguage(String language) {
        if (StringUtils.isBlank(language)) {
            return null;
        }
        if (StringUtils.contains(language, "_")) {
            language = StringUtils.lowerCase(StringUtils.substringAfterLast(language, "_"));
        }
        return language;
    }

    private String wrapMessage(List<AbnormalTradeNoticeLogBo> abnormalTradeNoticeLogList, String language) {
        String content = "";
        for (AbnormalTradeNoticeLogBo abnormalTradeNoticeLogBo : abnormalTradeNoticeLogList) {
            if (AbnromalTradeBusinessTypeEnum.AbnormalPrice == abnormalTradeNoticeLogBo.getBusinessType()) {
                content += AbnormalPricePushMessageConvertor.wrapMessage(abnormalTradeNoticeLogBo, language);
            }
            if (AbnromalTradeBusinessTypeEnum.PriceBreakthroughs == abnormalTradeNoticeLogBo.getBusinessType()) {
                content += PriceBreakthoughsPushMessageConvertor.wrapMessage(abnormalTradeNoticeLogBo, language);
            }
        }
        return content;
    }

    private List<AbnormalTradeNoticeLogBo> wrapNeedPushFlag(
            List<AbnormalTradeNoticeLogBo> abnormalTradeNoticeLogBoList) {
        List<AbnormalTradeNoticeLogBo> resultList = Lists.newArrayList();
        for (AbnormalTradeNoticeLogBo abnormalTradeNoticeLogBo : abnormalTradeNoticeLogBoList) {
            if (AbnromalTradeBusinessTypeEnum.AbnormalPrice == abnormalTradeNoticeLogBo.getBusinessType()) {
                resultList.add(abnormalPriceService.wrapNeedPushFlag(abnormalTradeNoticeLogBo));
            }
            if (AbnromalTradeBusinessTypeEnum.PriceBreakthroughs == abnormalTradeNoticeLogBo.getBusinessType()) {
                resultList.add(priceBreakthroughsService.wrapNeedPushFlag(abnormalTradeNoticeLogBo));
            }
        }
        return resultList;
    }


}
